import { _0, _9 } from "@/utils/num";

export interface Gpio {
    port: string,
    pin: number,
}

// PA0 => GPIOA 0
export function parsePort(name: string): Gpio {

    let pin = 0;
    if (name.length < 3) {
        throw `invalid name ${name}`;
    }

    const n = name.charCodeAt(2);
    if ((n < _0) || (n > _9)) {
        throw 'invalid pin';
    }

    for (let i = 2; i < name.length; i++) {
        const c = name.charCodeAt(i);
        if ((c < _0) || (c > _9)) {
            break;
        }
        pin *= 10;
        pin += c - _0;
    }
    const id = name.charAt(1);
    const port = `GPIO${id}`;
    return {
        port,
        pin
    };
}


